//
//  DSVideoShareView.m
//  DSVideo
//
//  Created by liusha on 2019/6/25.
//  Copyright © 2019 DS Technology. All rights reserved.
//

#import "DSVideoShareView.h"
#import <Masonry/Masonry.h>

@interface DSVideoShareViewCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation DSVideoShareViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.titleLabel];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.centerX.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
    }];
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [_titleLabel setFont:[UIFont systemFontOfSize:10]];
        [_titleLabel setTextColor:[UIColor whiteColor]];
    }
    return _titleLabel;
}

@end

@interface DSVideoShareView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *iconArray;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *continueButton;
@end

static NSString *kShareCellId = @"UICollectionViewCell";

@implementation DSVideoShareView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupViews];
        [self ds_updateConstraints];
    }
    return self;
}



- (void)setupViews {
    
//    CGFloat space = 20;
    NSArray *titleArray = @[@"微信", @"朋友圈", @"QQ", @"QQ空间", @"Facebook"];
    NSArray *iconArray = @[@"icon_share_wx",
                           @"icon_share_friend",
                           @"icon_share_qq",
                           @"icon_share_qzone",
                           @"icon_share_fb",
                           @"icon_share_tg"];
    self.titleArray = titleArray;
    self.iconArray = iconArray;
    
    [self addSubview:self.collectionView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.continueButton];
    
    
    [self.collectionView registerClass:[DSVideoShareViewCell class] forCellWithReuseIdentifier:kShareCellId];
    
}

- (void)ds_updateConstraints {
    __weak typeof(self) weakSelf = self;
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.offset(60);
        make.centerY.equalTo(@0);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.bottom.equalTo(weakSelf.collectionView.mas_top).offset(-10);
    }];
    
    [_continueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.top.equalTo(weakSelf.collectionView.mas_bottom).offset(20);
    }];
    
    _continueButton.layer.masksToBounds = YES;
    _continueButton.layer.borderColor = [UIColor whiteColor].CGColor;
    _continueButton.layer.borderWidth = 1;
    _continueButton.layer.cornerRadius = 12;
    
}


#pragma mark - Action
- (void)continueButtonClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(videoShareViewDidContinuePlayer:)]) {
        [self.delegate videoShareViewDidContinuePlayer:sender];
    }
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"indexPath");
    
//    [DSStatusHud showInfoWithString:[NSString stringWithFormat:@"分享到 %@", _titleArray[indexPath.item]]];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DSVideoShareViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kShareCellId forIndexPath:indexPath];
    
    [cell.imageView setImage:[UIImage imageNamed:_iconArray[indexPath.item]]];
    [cell.titleLabel setText:_titleArray[indexPath.item]];
    return cell;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    // cell 居中显示
    NSInteger numberOfItems = [collectionView numberOfItemsInSection:0];
    CGFloat combinedItemWidth = (numberOfItems * collectionViewLayout.itemSize.width) + ((numberOfItems - 1) * collectionViewLayout.minimumInteritemSpacing);
    CGFloat padding = (collectionView.frame.size.width - combinedItemWidth) / 2;
    padding = padding > 0 ? padding : 0;
    
    return UIEdgeInsetsMake(0, padding,0, padding);
}

#pragma mark - Lazy
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.itemSize = CGSizeMake(48, 55);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 15;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
    }
    return _collectionView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        [_titleLabel setFont:[UIFont systemFontOfSize:10]];
        [_titleLabel setTextColor:[UIColor whiteColor]];
        _titleLabel.text = @"分享视频至以下 方可继续观看";
    }
    return _titleLabel;
}

- (UIButton *)continueButton {
    if (!_continueButton) {
        _continueButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_continueButton setTitle:@"    继续播放    " forState:UIControlStateNormal];
        [_continueButton.titleLabel setFont:[UIFont systemFontOfSize:10]];
        [_continueButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_continueButton addTarget:self action:@selector(continueButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _continueButton;
}
@end
